package pers.liuchengyin.utils;

import pers.liuchengyin.enums.ErrorCode;
import pers.liuchengyin.pojo.Result;

/**
 * @ClassName ResultUtils
 * @Description
 * @Author 柳成荫
 * @Date 2022/3/27
 */
public class ResultUtils {

    private static final int SUCCESS_CODE = 0;

    public static <T> Result<T> successResult(T data) {
        Result<T> result = new Result<>();
        result.setSuccess(true);
        result.setCode(SUCCESS_CODE);
        result.setData(data);
        return result;
    }


    public static <T> Result<T> failResult(Integer code, String message) {
        Result<T> result = new Result<>();
        result.setCode(code);
        result.setSuccess(false);
        result.setMsg(message);
        return result;
    }

    public static <T> Result<T> failResult(Integer code, String message, T data) {
        Result<T> result = failResult(code, message);
        result.setData(data);
        return result;
    }

    public static <T> Result<T> failResult(ErrorCode errorCode) {
        return failResult(errorCode.getCode(), errorCode.getMsg());
    }

    public static <T> Result<T> failResult(ErrorCode errorCode, T data) {
        return failResult(errorCode.getCode(), errorCode.getMsg(), data);
    }

}
